<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			.obox{
				width: 400px;
				height: 400px;
				border: solid 1px black;
				margin: 50px auto;
				position: relative;
			}
			.box{
				width: 20px;
				height: 20px;
				background: blue;
				position: absolute;
				top: 100px;
				left: 50px;
			}
		</style>
		<script src="js/jquery-3.6.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			$(function(){
				var t1 = requestAnimationFrame(fn);
				var ox = Math.ceil(Math.random()*4);
				var oy = Math.ceil(Math.random()*4);
				function fn(){
					var x = $(".box").position().left;
					var y = $(".box").position().top;
					var w = $(".box").width();
					var h = $(".box").height();
					if(y>=$(".obox").height()-h){
						$(".box").css({"top":$(".obox").height()-h,"left":x+ox});
						oy = -Math.ceil(Math.random()*4);
					}else if(y<=0){
						$(".box").css({"top":0,"left":x+ox});
						oy = Math.ceil(Math.random()*4);
					}else if(x<=0){
						$(".box").css({"top":y+oy,"left":0});
						ox = Math.ceil(Math.random()*4);
					}else if(x>=$(".obox").width()-w){
						$(".box").css({"top":y+oy,"left":$(".obox").width()-w});
						ox = -Math.ceil(Math.random()*4);
					}
					$(".box").css({"top":y+oy,"left":x+ox});
					requestAnimationFrame(fn);
				}
			})
		</script>
	</head>
	<body>
		<div class="obox">
			<div class="box"></div>
		</div>
	</body>
</html>
